package com.yb.sys.user.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.yb.core.bean.Resp;
import com.yb.sys.user.bean.Students;
import com.yb.sys.user.repository.StudentsDao;
import com.yb.sys.user.service.IStudentService;
import com.yb.sys.user.vo.PageReq;
import com.yb.sys.user.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/11/23 14:14
 * FileName: StudentServiceImpl
 * Description:
 */

@Service
@Slf4j
public class StudentServiceImpl implements IStudentService {

    @Autowired
    StudentsDao studentsDao;

    @Override
    public Resp init() {

        List<Students> list=new ArrayList<>();
        for(int i=0;i<100;i++){

            String id=String.valueOf(i+1);
            list.add(Students.builder()._id(id).id(id).name("name"+id).age(i%18).build());
        }

        studentsDao.saveAll(list);
        return Resp.ok();
    }

    @Override
    public PageResult page(PageReq page) {
        log.info("page=sortItems={},sort={}", JSONObject.toJSONString(page.getSorts()),page.getSort());
        PageRequest pageRequest = PageRequest.of (page.getPage()-1, page.getSize(), page.getSort());
        Page<Students> resultPage = studentsDao.findAll(pageRequest);
        return new PageResult().pageReq(page).resultPage(resultPage);
    }

}
